<?php
App::uses('AppController', 'Controller');
/**
 * Promos Controller
 *
 * @property Promo $Promo
 */
class PromosController extends AppController {

	public $components = array('RequestHandler');
	//var $uses = array('Promos','Stores');
	public function map() {
	
		$latlng = $this->Promo->query("select latitude,longitude from stores");
		$this->set(compact('latlng'));
	}
	
	public function beforeFilter() {
		parent::beforeFilter ();
		if ($this->RequestHandler->accepts('html')) {
			// Execute code only if client accepts an HTML (text/html) response
		} elseif ($this->RequestHandler->accepts('xml')) {
			// Execute XML-only code
		}
	
		$this->Auth->allow ( '*' );
	}
	
	public function ajaxQueryPromos() {
		$id=$this->params['url']['category_id'];
		if($id<5){
			$queryPromo = $this->Promo->query("select * from categories as A , store_categories as B , stores as C , promos as D where A.id = B.category_id and B.store_id = C.id and A.id = '$id' and C.status = '1' and C.id = D.store_id and D.begin_date <= NOW() and D.end_date >= NOW()");
		}else{
			$queryPromo = $this->Promo->query("select * from stores as C , promos as D where C.status = '1' and C.id = D.store_id and begin_date <= NOW() and end_date >= NOW()");
		}
		$this->layout = 'ajax';
		$this->Promo->recursive = 0;
		$this->set ( compact ( 'queryPromo' ) );
	}
	
	public function ajaxQueryStoreDetails() {
		$id=$this->params['url']['s_id'];
		$queryPromo = $this->Promo->query("select * from stores as C , promos as D where C.status = '1' and C.id = D.store_id and C.id = '$id' and D.begin_date <= NOW() and D.end_date >= NOW()");
		$this->layout = 'ajax';
		$this->Promo->recursive = 0;
		$this->set ( compact ( 'queryStoreDetail' ) );
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Promo->recursive = 0;
		$this->set('promos', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Promo->id = $id;
		if (!$this->Promo->exists()) {
			throw new NotFoundException(__('Invalid promo'));
		}
		$this->set('promo', $this->Promo->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Promo->create();
			if ($this->Promo->save($this->request->data)) {
				$this->Session->setFlash(__('The promo has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The promo could not be saved. Please, try again.'));
			}
		}
		$users = $this->Promo->User->find('list');
		$stores = $this->Promo->Store->find('list');
		$this->set(compact('users', 'stores'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Promo->id = $id;
		if (!$this->Promo->exists()) {
			throw new NotFoundException(__('Invalid promo'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Promo->save($this->request->data)) {
				$this->Session->setFlash(__('The promo has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The promo could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Promo->read(null, $id);
		}
		$users = $this->Promo->User->find('list');
		$stores = $this->Promo->Store->find('list');
		$this->set(compact('users', 'stores'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Promo->id = $id;
		if (!$this->Promo->exists()) {
			throw new NotFoundException(__('Invalid promo'));
		}
		if ($this->Promo->delete()) {
			$this->Session->setFlash(__('Promo deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Promo was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
